﻿Public Class FrmHinhThucThue
    Private selectedmaht As String
    Private Sub phuchoi()
        btnSua.Text = "&Sửa"
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        phuchoi()
        txtHTT.Text = ""

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Hide()
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        phuchoi()
        If txtHTT.Text <> "" Then
            If Not HinhThucThue.them(txtHTT.Text) Then
                MsgBox("Đã có hình thức thuê này", MsgBoxStyle.Critical, "Lỗi")

            End If
            dgvHTT.DataSource = HinhThucThue.dsht()
            btnNhapLai_Click(sender, e)
        Else
            MsgBox("Bạn cần điền đầy đủ thông tin vào !", MsgBoxStyle.Information, "Thông báo")

        End If

    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        phuchoi()
        dgvHTT.DataSource = HinhThucThue.timkiem(txtTuKhoa.Text)
    End Sub

    Private Sub btndaydu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndaydu.Click

        dgvHTT.DataSource = HinhThucThue.dsht()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            selectedmaht = dgvHTT.CurrentRow.Cells("MaHT").Value
            txtHTT.Text = dgvHTT.CurrentRow.Cells("TenHT").Value
            btnSua.Text = "&Lưu "
        Else
            If Not HinhThucThue.capnhat(selectedmaht, txtHTT.Text) Then
                MsgBox("Không thể lưu được dữ liệu", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvHTT.DataSource = HinhThucThue.dsht()
            btnNhapLai_Click(sender, e)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        btnSua.Text = "&Sửa"
        If MsgBox("Bạn chắc chắn muốn xóa hình thức thuê này chứ?", MsgBoxStyle.YesNo, " Thông báo") = DialogResult.Yes Then
            If Not HinhThucThue.xoa(dgvHTT.CurrentRow.Cells("MaHT").Value) Then
                MsgBox("Bạn không thể xóa hình thức thuê này", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvHTT.DataSource = HinhThucThue.dsht()
            btnNhapLai_Click(sender, e)

        End If
    End Sub

    Private Sub dgvHTT_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHTT.CellDoubleClick
        btnSua.Text = "&Sửa"
        btnSua_Click(sender, e)
    End Sub

    Private Sub dgvHTT_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHTT.CellContentClick

    End Sub

    Private Sub FrmHinhThucThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvHTT.DataSource = HinhThucThue.dsht
    End Sub
End Class